package com.gexiaobao.pigeon.ui.dialog

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.ext.init
import com.gexiaobao.pigeon.app.model.bean.DeviceListBean
import com.gexiaobao.pigeon.app.util.Util
import com.gexiaobao.pigeon.app.weight.recycleview.SpaceItemDecoration
import com.gexiaobao.pigeon.databinding.BottomDeviceCodeDialogBinding
import com.gexiaobao.pigeon.ui.adapter.DeviceCodeListAdapter
import com.google.android.material.bottomsheet.BottomSheetDialogFragment

/**
 * created by : hxw
 * @date : 20221130
 * Describe :
 */
class BottomDeviceCodeDialog(
    private val mDataList: List<DeviceListBean.DataList>,
    val title: String
) : BottomSheetDialogFragment() {

    private lateinit var binding: BottomDeviceCodeDialogBinding

    //    private var mPosition: Int = -1
//    private var mSelectText: String = ""
    private val mAdapter: DeviceCodeListAdapter by lazy { DeviceCodeListAdapter(arrayListOf()) }

    /**
     * setStyle 圆角效果
     */
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NORMAL, R.style.BottomSheetDialog)
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = BottomDeviceCodeDialogBinding.inflate(inflater)
        initView()
        return binding.root
    }

    private fun initView() {
        binding.swipeRecyclerView.init(LinearLayoutManager(context), mAdapter)
            .addItemDecoration(SpaceItemDecoration(0, Util.dp2px(context, 2f)))
        mAdapter.setList(mDataList)
        binding.swipeRecyclerView.adapter = mAdapter

        binding.tvPigeonDialogTitle.text = title
        binding.tvPigeonColorCancel.setOnClickListener {
            dismiss()
        }

//        binding.tvPigeonColorSure.setOnClickListener {
//            onItemClick?.setOnItemClickListener(mSelectText, mPosition)
//            dismiss()
//        }

        mAdapter.run {
            setOnItemClickListener { _, _, position ->
                val code = mDataList[position].imsi
                onItemClick?.setOnItemClickListener(code)
            }
        }
    }

    interface OnItemClickCode {
        fun setOnItemClickListener(content: String)
    }

    private var onItemClick: OnItemClickCode? = null

    fun setOnItemClickListener(onItemClickRe: OnItemClickCode) {
        this.onItemClick = onItemClickRe
    }
}